#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#include"AdminManager.h"
#include"Administer.h"

AdminManager::AdminManager()
{
	this->logIndex = -1;
#pragma region 检测文件是否存在和是否含有数据

	ifstream ifs;

	ifs.open("administer.txt", ios::in);

	if (!ifs.is_open())
	{
		cout << "管理员文件打开失败" << endl;
		//初始化
		this->AdminNum = 0;
		this->IsFileEmpty = true;
		this->AdminArray = NULL;
		ifs.close();
	}

	char ch;
	ifs >> ch;

	if (ifs.eof())
	{
		cout << "管理员数据文件为空" << endl;
		//初始化
		this->AdminNum = 0;
		this->IsFileEmpty = true;
		this->AdminArray = NULL;
		ifs.close();
	}

	//读取管理员数量
	int num = this->countAdminNum();
	/*cout << "当前有管理员" << num << "人" << endl;*/
	this->AdminNum = num;

	//创建管理员数组
	this->AdminArray = new Administer * [this->AdminNum];
	initAdmin();

	
	/*ifs.open("book.txt", ios::in);

	if (!ifs.is_open())
	{
		cout << "书本文件打开失败" << endl;
	}
	
	ifs >> ch;

	if (ifs.eof())
	{
		cout << "书籍数据文件为空" << endl;
		ifs.close();
	}*/

#pragma endregion	

}

AdminManager::~AdminManager()
{
	//清除内存空间
	if (AdminArray != NULL)
	{
		delete[] this->AdminArray;
		this->AdminArray = NULL;
	}
}

void AdminManager::log()
{
	this->showAllAdmin();

	string logNum;
	cout << "请输入要登陆的管理员序号" << endl;
	cin >> logNum;

	bool flag = false;
	for (int i = 0; i < this->AdminNum; i++)
	{
		if (this->AdminArray[i]->getNum() == logNum)
		{
			flag = true;
			logIndex = i;
		}		
	}

	if (flag)
	{
		cout << "请输入密码" << endl;
		string passWord;
		cin >> passWord;
		if (passWord == this->AdminArray[logIndex]->getPassWord())
		{
			cout << "登陆成功！" << endl;
			system("pause");
		}
		else
		{
			cout << "密码错误，登陆失败，请重试！" << endl;
			logIndex = -1;
			system("pause");
			return;
		}
	}
	else
	{
		cout << "管理员序号输入错误，请重试" << endl;
		system("pause");
		return;
	}

	
}

void AdminManager::addAdminister()
{
	int newSize = this->AdminNum + 1;

	Administer** newSpace = new Administer * [newSize];

	//将原数列复制进新数列
	if (this->AdminArray != NULL)
	{
		for (int i = 0; i < this->AdminNum; i++)
		{
			newSpace[i] = this->AdminArray[i];
		}
	}

	//输入新管理员信息
	string num;
	string passWord;
	string name;

	cout << "请输入新管理员编号" << endl;
	cin >> num;
	bool flag;
	//检测与之前的管理员序号是否有重复
	do
	{
		flag = true;

		for (int i = 0; i < this->AdminNum; i++)
		{
			if (this->AdminArray == NULL)
			{
				break;
			}
			if (this->AdminArray[i]->getNum() == num)
			{
				flag = false;
			}
		}

		if (flag == false)
		{
			cout << "此编号已被占用，请重新输入" << endl;
			cin >> num;
		}

	} while (flag == false);
	
	cout << "请输入新管理员密码" << endl;
	cin >> passWord;

	cout << "请输入新管理员姓名" << endl;
	cin >> name;

	//将新管理员加入数组
	Administer* administer = new Administer(num, passWord, name);
		
	newSpace[this->AdminNum] = administer;
	
	//更新数组
	delete[] this->AdminArray;

	this->AdminArray = newSpace;

	this->AdminNum = newSize;

	system("pause");

	this->saveAdminister();

	return;

	/*Administer* newAdptr = new Administer;

	cout << "请输入新管理员编号" << endl;
	string nu;
	cin >> nu;
	newAdptr->setNum(nu);

	cout << "请输入新管理员姓名" << endl;
	string nam;
	cin >> nam;
	newAdptr->setName(nam);

	cout << "请输入新管理员密码" << endl;
	string password;
	cin >> password;
	newAdptr->setPassWord(password);

	this->saveAdminister(newAdptr);
	delete newAdptr;*/
}

void AdminManager::saveAdminister()
{
	ofstream ofs;
		
	ofs.open("administer.txt", ios::out);
		
	for (int i = 0; i < this->AdminNum; i++)
	{
		ofs << this->AdminArray[i]->getNum() << " "
			<< this->AdminArray[i]->getPassWord() << " "
			<< this->AdminArray[i]->getName() << endl;
	}
	
	ofs.close();
	
}

int AdminManager::countAdminNum() 
{
	ifstream ifs;
	ifs.open("administer.txt", ios::in);
	
	string num;
	string password;
	string name;

	int count = 0;
	
	while(ifs >> num && ifs >> password && ifs >> name)
	{
		count++;
	}

	ifs.close();

	return count;
}

void AdminManager::deleteAdmin()
{
	cout << "请输入要删除的管理员编号" << endl;
	string num;
	cin >> num;

	//查找并返回管理员在数组中的位置
	int index = this->searchByNum(num);
		
	if (index != -1)
	{
		cout << "请输入要删除的管理员的密码" << endl;
		string password;
		cin >> password;

		if (password == this->AdminArray[index]->getPassWord())
		{
			for (int i = index; i < this->AdminNum - 1; i++)
			{
				this->AdminArray[i] = this->AdminArray[i + 1];
			}

			//更新人员数目
			this->AdminNum--;
			//保存数据
			this->saveAdminister();

			cout << "删除成功！" << endl;
		}
		else
		{
			cout << "输入的密码错误，删除失败" << endl;
		}
	}
	else
	{
		cout << "未找到该编号的管理员，请重试" << endl;
	}

	system("pause");
}

void AdminManager::initAdmin()
{
	ifstream ifs;
	ifs.open("administer.txt", ios::in);

	string num;
	string password;
	string name;

	int index = 0;
	while (ifs >> num && ifs >> password && ifs >> name)
	{
		this->AdminArray[index] = new Administer(num, password, name);

		index++;
	}

}

void AdminManager::showAllAdmin()
{
	for (int i = 0; i < this->AdminNum; i++)
	{
		this->AdminArray[i]->showInfo();

	}

	system("pause");
}

void AdminManager::showAdmin(int index)
{
	if (index != -1)
	{
		this->AdminArray[index]->showInfo();
	}
	else
	{
		cout << "未找到该管理员" << endl;
	}
	system("pause");
}

int AdminManager::searchByNum(string num)
{
	int index = -1;

	for (int i = 0; i < this->AdminNum; i++)
	{
		if (this->AdminArray[i]->getNum() == num)
		{
			index = i;
			break;
		}		
	}

	return index;
}

int AdminManager::searchByName(string name)
{
	int index = -1;

	for (int i = 0; i < this->AdminNum; i++)
	{
		if (this->AdminArray[i]->getName() == name)
		{
			index = i;
			break;
		}
	}

	return index;
}

void AdminManager::changePassWord()
{
	cout << "请输入新密码" << endl;
	string password;
	cin >> password;

	this->AdminArray[logIndex]->setPassWord(password);
}
